package com.qf.utils;

import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * @Description: 公用常量
 * @Data: 2022/9/1 23:01
 * @Pacakge: com.qf.utils
 * @ClassName: TransactionManager
 * @Author: fan
 */
@Component
@Aspect//声明当前类为切面类/增强类
public class TransactionManager {
    @Autowired
    private ConnectionUtils connectionUtils;
    @Pointcut(value = "execution(* com.qf.service.impl.AccountServiceImpl.transfer(..))")
    public void pointcut(){

    }
    //开启事务
    @Before(value = "pointcut()")
    public void beginTransaction(){
        try {
            connectionUtils.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //提交事务
    @AfterReturning(value = "pointcut()")
    public void commitTransaction(){
        try {
            connectionUtils.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //事务回滚
    @AfterThrowing(value = "pointcut()")
    public void rollbackTransaction(){
        try {
            connectionUtils.getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //释放资源
    @After(value = "pointcut()")
    public void release(){
        try {
            connectionUtils.getConnection().close();
            connectionUtils.removeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
